<?php

/**
 * DAO for inserting Tasks into the database.
 */
class TaskDAO {
    
    public function insert(Task $task){
        // Get database connection
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);
        
        // Task Type is hard coded for now. 1 is for Notification.
        $query = 'INSERT INTO notification ';
        $query .= '(NOTIFICATION_TYPE, title, content, creator, important, deadline) VALUES ';
        $query .= '(2, \''.mysqli_real_escape_string($db, $task->getTitle()).'\', \''.mysqli_real_escape_string($db, $task->getContent()).'\', \''.mysqli_real_escape_string($db, $task->getCreator()).'\', \''.mysqli_real_escape_string($db, $task->getImportant()).'\', \''.mysqli_real_escape_string($db, $task->getDeadline()).'\');';
        
        return mysqli_query($db, $query) ? mysqli_insert_id($db) : false;
    }
    
    public function update(Task $task){
        // Get database connection
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);
        
        $query = 'UPDATE notification SET title = \''.mysqli_real_escape_string($db, $task->getTitle()).'\',';
        $query .= 'content = \''.mysqli_real_escape_string($db, $task->getContent()).'\',';
        $query .= 'creator = \''.mysqli_real_escape_string($db, $task->getCreator()).'\',';
        $query .= 'important = \''.mysqli_real_escape_string($db, $task->getImportant()).'\',';
        $query .= 'deadline = \''.mysqli_real_escape_string($db, $task->getDeadline()).'\',';
        $query .= 'modified = \''.mysqli_real_escape_string($db, $task->getModified()).'\',';
        $query .= 'archived = \''.mysqli_real_escape_string($db, $task->getArchived()).'\' ';
        $query .= 'WHERE NID = '.mysqli_real_escape_string($db, $task->getId()).';';
        
        return mysqli_query($db, $query);
    }
    
    /**
     * Get all Task items.
     * 
     * Deleted and archived items will not be returned.
     * Items are ordered by priority and date.
     * 
     * @return type 
     */
    public function getAll($importantfirst = true, $archived = false, $deleted = false){
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);

        $resultset = array();
        
        $query = "SELECT * FROM notification WHERE NOTIFICATION_TYPE = 2 ";
        if(!$deleted){
            $query .= "AND deleted = 0 ";
        }
        if(!$archived){
            $query .= "AND archived = 0 ";
        }
        if($importantfirst){
            $query .= "ORDER BY important DESC, deadline ASC;";
        } else {
            $query .= "ORDER BY deadline ASC;";
        }
        
        $result = mysqli_query($db, $query);
        
        echo mysqli_error($db);
        
        while($row = mysqli_fetch_assoc($result)){
            $resultset[] = $this->createFromRow($row);
        }
        
        return $resultset;
    }
    
    public function getCurrent($importantfirst = true, $archived = false, $deleted = false){
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);

        $resultset = array();
        
        $deadline_limit = date("Y-m-d", strtotime(date("Y-m-d", time())) + (3*24*60*60));
        
        $query = "SELECT * FROM notification WHERE NOTIFICATION_TYPE = 2 AND deadline < '$deadline_limit' ";
        if(!$deleted){
            $query .= "AND deleted = 0 ";
        }
        if(!$archived){
            $query .= "AND archived = 0 ";
        }
        if($importantfirst){
            $query .= "ORDER BY important DESC, deadline ASC;";
        } else {
            $query .= "ORDER BY deadline ASC;";
        }
        
        $result = mysqli_query($db, $query);
        
        echo mysqli_error($db);
        
        while($row = mysqli_fetch_assoc($result)){
            $resultset[] = $this->createFromRow($row);
        }
        
        return $resultset;
    }
    
    /**
     * Get single task by ID.
     * 
     * @param int $id ID of the required notification.
     * @return Task
     */
    public function get($id){
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);

        $resultset = array();
        
        $id = mysqli_real_escape_string($db, $id);
        
        $query = "SELECT * FROM notification WHERE NOTIFICATION_TYPE = 2 AND NID = $id;";
        $result = mysqli_query($db, $query);
        
        echo mysqli_error($db);
        
        while($row = mysqli_fetch_assoc($result)){
            $resultset[] = $this->createFromRow($row);
        }
        
        return count($resultset) > 0 ? $resultset[0] : null;
    }
    
    private function createFromRow($row){
        $task = new Task();
        $task->setAdded($row['added']);
        $task->setArchived($row['archived']);
        $task->setContent($row['content']);
        $task->setCreator($row['creator']);
        $task->setDeleted($row['deleted']);
        $task->setEditor($row['editor']);
        $task->setId($row['NID']);
        $task->setImportant($row['important']);
        $task->setTitle($row['title']);
        $task->setDeadline($row['deadline']);
        $task->setModified($row['modified']);
        $task->setType($row['NOTIFICATION_TYPE']);
        
        return $task;
    }
}

?>
